package com.yanqu.road.server.cmd.activity.tonbingchannel.channel;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.utils.property.PropertyHelper;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = GameProtocol.S_CHANNEL_CTON_BING_CALL_SOLDIER_SLIVER_RESP, desc = "")
public class TonBingCallSoldierSliverRespCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        ChannelTonBingProto.ChannelTonBingRecruitSoldierReqMsg respMsg = ChannelTonBingProto.ChannelTonBingRecruitSoldierReqMsg.parseFrom(packet.getBytes());
        long userId = packet.getPlayerId();
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player != null) {

            String costSilver = respMsg.getSilver();
            String cost = GameConfig.GAME_MONEY_SILVER + "=" + costSilver;

            // 消耗判断
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(cost))) {
                ChannelTonBingProto.CTonBingRecruitSoldierRespMsg.Builder respUserMsg = ChannelTonBingProto.CTonBingRecruitSoldierRespMsg.newBuilder();
                respUserMsg.setRet(GameErrorCode.E_BAG_SILVER_NO_ENOUGH);
                player.sendPacket(ClientProtocol.U_CTON_BING_RECRUIT_SOLDIER, respUserMsg);
                return;
            }

            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(cost);

            // 跨服处理
            ChannelTonBingProto.ChannelTonBingRecruitSoldierReqMsg.Builder builder = respMsg.toBuilder();
            builder.setReqType(1);
            player.sendChannelPlayerPacket(ChannelProtocol.N_TON_BING_CALL_SOLDIER, builder);
        }
    }

}
